﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
namespace KMS.Core
{
    using System.Data;
    using System.Data.SqlClient;

    public partial class ServiceBase<T>
    {
        private const string Filed_LastModifier = "LastModifier";
        private const string Filed_LastModifyTime = "LastModifyTime";
        private const string Filed_Creator = "Creator";
        private const string Filed_CreateTime = "CreateTime";

        private void QueryExcute(Action<IDbConnection> action)
        {
            using (var db = new SqlConnection(this.DbConnectString))
            {
                try
                {
                    db.Open();
                    if (action != null)
                    {
                        action(db);
                    }
                }
                catch (Exception)
                {

                    // throw;
                }
                finally
                {
                    db.Close();
                }
            }
           
        }

        //private static Dictionary<string, object> GetTimeAndPersonForCreate()
        //{
        //    string user = "Current UserName"; // todo
        //    var dict = new Dictionary<string, object>
        //               {
        //                   { Filed_LastModifier, user },
        //                   { Filed_LastModifyTime, DateTime.Now },
        //                   { Filed_Creator, user },
        //                   { Filed_CreateTime, DateTime.Now }
        //               };
        //    return dict;
        //}
        //private static Dictionary<string, object> GetTimeAndPersonForUpdate()
        //{
        //    string user = "Current UserName"; // todo
        //    var dict = new Dictionary<string, object>
        //        {
        //            {Filed_LastModifier, user},
        //            {Filed_LastModifyTime, DateTime.Now}
        //        };
        //    return dict;
        //}
        //protected IInsertBuilder BuilderParse(ParamInsert param)
        //{
        //    var data = param.GetData();
        //    var insertBuilder = Db.Insert(data.From.Length == 0 ? this.TableName : data.From);

        //    var dict = GetTimeAndPersonForCreate();

        //    foreach (var column in data.Columns.Where(column => !dict.ContainsKey(column.Key)))
        //        insertBuilder.Column(column.Key, column.Value);

        //    var properties = typeof(T).GetTypeProperties().Select(pi => pi.GetColumnName().ToLower()).ToList();

        //    foreach (var item in dict.Where(item => properties.Contains(item.Key.ToLower()))) { 
        //        insertBuilder.Column(item.Key, item.Value);
        //    }
        //    return insertBuilder;
        //}

        //protected IInsertBuilder<T> BuilderInsertParse(T obj)
        //{
        //    var insertBuilder = Db.Insert(this.TableName, obj);
        //    return insertBuilder;
        //}
        //protected IUpdateBuilder<T> BuilderUpdateParse(T obj)
        //{
        //    var updateBuilder = Db.Update(this.TableName, obj);
        //    return updateBuilder;
        //}
        //protected IUpdateBuilder BuilderParse(ParamUpdate param)
        //{
        //    var data = param.GetData();
        //    var updateBuilder = Db.Update(data.Update.Length == 0 ? this.TableName : data.Update);

        //    var dict = GetTimeAndPersonForUpdate();

        //    foreach (var column in data.Columns.Where(column => !dict.ContainsKey(column.Key)))
        //    {
        //        updateBuilder.Column(column.Key, column.Value);
        //    } 
        //    var properties = typeof(T).GetTypeProperties().Select(pi => pi.GetColumnName().ToLower()).ToList();

        //    foreach (var item in dict.Where(item => properties.Contains(item.Key.ToLower())))
        //    {
        //        updateBuilder.Column(item.Key, item.Value);
        //    }
        //    updateBuilder.Where(data.WhereSql);
        //    foreach (var o in data.WherePara)
        //    {
        //        updateBuilder.Parameter(o.Key, o.Value);
        //    }
        //    return updateBuilder;
        //}

        //protected IDeleteBuilder BuilderParse(ParamDelete param)
        //{
        //    var data = param.GetData();
        //    var deleteBuilder = Db.Delete(data.From.Length == 0 ? this.TableName : data.From);
        //    deleteBuilder.Where(data.WhereSql);
        //    foreach (var o in data.WherePara)
        //    {
        //        deleteBuilder.Parameter(o.Key, o.Value);
        //    }
          
        //    return deleteBuilder;
        //}

        //protected IStoredProcedureBuilder BuilderParse(ParamSP param)
        //{
        //    var data = param.GetData();
        //    var spBuilder =Db.StoredProcedure(data.Name);
        //    foreach (var item in data.Parameter)
        //        spBuilder.Parameter(item.Key, item.Value);

        //    foreach (var item in data.ParameterOut)
        //        spBuilder.ParameterOut(item.Key, item.Value);

        //    return spBuilder;
        //}

        //protected ISelectBuilder<T> BuilderParse(ParamQuery param)
        //{
        //    if (param == null)
        //    {
        //        param = new ParamQuery();
        //    }

        //    var data = param.GetData();
        //    var sFrom = data.From.Length == 0 ? typeof(T).Name : data.From;
        //    var selectBuilder = Db.Select<T>(string.IsNullOrEmpty(data.Select) ? (sFrom + ".*") : data.Select)
        //        .From(sFrom)
        //        .Where(data.WhereSql)
        //        .GroupBy(data.GroupBy)
        //        .Having(data.Having)
        //        .OrderBy(data.OrderBy)
        //        .Paging(data.PagingCurrentPage, data.PagingItemsPerPage);
        //    foreach (var o in data.WherePara)
        //    {
        //        selectBuilder.Parameter(o.Key, o.Value);
        //    }
        //    return selectBuilder;
        //}
    }
}
